import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '@/views/Login.vue'
import Main from '@/views/Main.vue'
import Wel from '@/views/Welcome.vue'
import UserList from '@/views/User/UserListView.vue'
import UserAdd from '@/views/User/UserAddView.vue'
import RoleList from '@/views/Role/RoleListView.vue'
import RoleRights from '@/views/Role/RoleRightsView.vue'
import Categories from '@/views/Goods/Categories.vue'

Vue.use(VueRouter)

const routes = [
	{
		path: '/',
		redirect: '/login',
	},
	{
		path: '/login',
		component: Login,
		name: 'login'
	},
	{
		path: '/main',
		component: Main,
		children: [
			{
				path: '/',
				redirect: '/welcome',
			},
			{
				path: '/welcome',
				component: Wel,
				name: 'welcome'
			},
			{
				path: '/users',
				component: UserList,
				name: 'users'
			},
			{
				path: '/useradd',
				component: UserAdd,
				name: 'useradd'
			},
			{
				path: '/roles',
				component: RoleList,
				name: 'rolelist'
			},
			{
				path: '/rights',
				component: RoleRights,
				name: 'rolerights'
			},
			{
				path: '/categories',
				component: Categories,
				name: 'categories'
			},
		]
	}
]

const router = new VueRouter({
	routes
})

router.beforeEach((to, from, next) => {
	if (to.path !== '/' && to.path !== '/login') {
		if (!sessionStorage.getItem('token')) { next('/login'); }
	}
	next();
})

export default router
